<script>
    var __AddUrl   = '{{ .CommonIn.GetUrlByKey "admin/department/add" }}?parentId=';
    var __EditUrl   = '{{ .CommonIn.GetUrlByKey "admin/department/edit" }}?resId=';
    var __DelUrl    = '{{ .CommonIn.GetUrlByKey "admin/department/DoDel" }}';
    var __ListUrl  = '{{ .CommonIn.GetUrlByKey "admin/department/DoList" }}';
    function delData (departmentId){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: __DelUrl,
            data: {resId:departmentId},
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        window.location.reload()
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            }
        });
    }
    let EleTree ;
    function openAddPanel(pid){
        let index = layer.open({
            title: '添加子部门',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __AddUrl+pid,
        });
    }
    function openEditPanel(resId){
        let index = layer.open({
            title: '编辑部门信息',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __EditUrl+resId,
        });
    }

    function initData (eleTree,data){

        let obj={
            elem: '.eleTree',
            data: data,
            showLine:true,
            // url: "../eleTree/tree.json",
            renderAfterExpand: true,
            expandOnClickNode: true,
            contextmenuList:[{eventName: "editDept", text: "编辑部门"},{eventName: "addChildDept", text: "添加子部门"},{eventName: "delDept", text: "删除部门"}]
        }
        $("#btn-add").click(function(e){
            e.preventDefault();
            openAddPanel(-1);
        });
        //添加子部门
        eleTree.on("nodeAddChildDept",function(d) {
            openAddPanel(d.data.id);
        })
        //删除部门
        eleTree.on("nodeDelDept",function(d) {

            layer.confirm("确认删除该记录？",{
                title:"操作确认",
                yes:function(index,layero){
                    delData(d.data.id)
                    layer.close(index)
                },
                cancel:function(index){
                }

            })
        })
        //编辑部门信息
        eleTree.on("nodeEditDept",function(d) {
            openEditPanel(d.data.id)
        })
        EleTree = eleTree.render(obj);
    }
    layui.use(['eleTree'], function () {
        let eleTree = layui.eleTree;

        $.ajax({
            type: "get",
            url: __ListUrl,
            success: function (res) {
                let treeData = listToTree("id","parentId",res.data)
                if(res.code==0){
                    initData(eleTree,treeData)
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            },
            complete:function (){
                layer.closeAll();
            }
        });

        //el1.append(d,newData)

    });

</script>
